package com.app.xinfadi.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Handler;
import android.widget.TextView;

import com.app.xinfadi.R;
import com.app.xinfadi.util.DateTimeUtils;
import com.app.xinfadi.util.LogUtil;

import java.lang.ref.WeakReference;
import java.util.TimerTask;


/**
 * 类土司提示框
 *
 * @author WuTengFei <br/>
 * @Function: TODO <br/>
 * @date 2015-6-4 下午2:20:12 <br/>
 */
public class ToastDialog extends Dialog {
    private final static int DISMISS = 1;
    private static boolean flag = true;
    private static ToastDialog toastDialog;
    private TextView messageTv;
    private String TAG = ToastDialog.class.getSimpleName();
    private OnMyDissmissListener onMyDissmissListener;
    private MyHandler mHandler = new MyHandler(this);

    public ToastDialog(Context context) {
        super(context, R.style.CustomDialog);
        this.setContentView(R.layout.toast_dialog);
        messageTv = (TextView) findViewById(R.id.messageTv);
    }

    public ToastDialog(Context context, int theme) {
        super(context, theme);
    }

    public static ToastDialog createDialog(Context context, OnMyDissmissListener onMyDissmissListener) {
        toastDialog = new ToastDialog(context, R.style.CustomDialog);
        toastDialog.setContentView(R.layout.toast_dialog);
        toastDialog.setOnMyDissmissListener(onMyDissmissListener);

        return toastDialog;
    }

    public static ToastDialog createDialog(Context context) {
        toastDialog = new ToastDialog(context, R.style.CustomDialog);
        toastDialog.setContentView(R.layout.toast_dialog);
        return toastDialog;
    }

    @Override
    public void show() {
        super.show();
    }

    public void delayHideShow() {
        mHandler.sendEmptyMessageDelayed(DISMISS, 2000);
    }

    public void setMessage(String message) {
        TextView messageTv1 = (TextView) toastDialog.findViewById(R.id.messageTv);
        if (messageTv1 != null)
            messageTv1.setText(message);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        flag = false;
    }

    public OnMyDissmissListener getOnMyDissmissListener() {
        return onMyDissmissListener;
    }

    public void setOnMyDissmissListener(OnMyDissmissListener onMyDissmissListener) {
        this.onMyDissmissListener = onMyDissmissListener;
    }

    public interface OnMyDissmissListener {
        public void onDissmiss();
    }

    static class MyHandler extends Handler {
        WeakReference<ToastDialog> outerClass;


        MyHandler(ToastDialog theClass) {
            outerClass = new WeakReference<ToastDialog>(theClass);
        }

        @Override
        public void handleMessage(android.os.Message msg) {
            ToastDialog theClass = outerClass.get();
            switch (msg.what) {
                case DISMISS:
                    LogUtil.d(theClass.TAG, "dissmiss");
                    theClass.dismiss();
                    if (theClass.onMyDissmissListener != null) {
                        theClass.onMyDissmissListener.onDissmiss();
                    }
                    break;
                default:
                    break;
            }
        }
    }

    class MyTask extends TimerTask {
        @Override
        public void run() {
            while (flag) {
                LogUtil.d(TAG, "MyTask:" + DateTimeUtils.getNowDetaText());
                // Message message = new Message();
                // message.what = DISMISS;
                // mHandler.sendMessage(message);
            }
        }
    } // 定时读取消息

}
